﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Sharp.Domain.Model;
using Sharp.Domain.Repository.EF;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Data;
using Sharp.Infrastructure.Search;

namespace Sharp.Domain.PlatformBoundedContext.MessageModule.Aggregates
{
    public partial class MessageRepository
    {
        public override void Remove(Message aggregateRoot)
        {
            var ef = (this.Context as IEntityFrameworkRepositoryContext).Context;
            var info = Get(aggregateRoot.Id);
            if (info.EntityState != EntityStateOperate.IsDeleted)//如果原先不是逻辑删除就标记为逻辑删除
            {
                info.EntityState = EntityStateOperate.IsDeleted;
            }
            if (info.EntityState == EntityStateOperate.IsDeleted)//如果已经是逻辑删除就执行物理删除
            {
                ef.Set<Message>().Remove(info);
            }

            //别的地方执行提交
        }

        /// <summary>
        /// 发件箱
        /// </summary>
        /// <param name="pf"></param>
        /// <param name="me"></param>
        /// <returns></returns>
        public PagedResult<Message> GetOutboxPagging(PagingFeature pf, Guid me)
        {
            var ef = (this.Context as IEntityFrameworkRepositoryContext).Context;
            ef.Configuration.LazyLoadingEnabled = false;

            var pagepredicate = new QuerySupport<Message>().TobuildFilter(pf);


            var query = ef.Set<Message>().Filter().Where(x => x.SenderId == me);
            if (pagepredicate != null)
            {
                query = query.Where(pagepredicate);
            }

            query = query.OrderByDescending(x => x.Id);

            return new PagedResult<Message>(query, pf.PageSize, pf.PageNo);
        }
        /// <summary>
        /// 草稿箱
        /// </summary>
        /// <param name="pf"></param>
        /// <param name="me"></param>
        /// <returns></returns>
        public PagedResult<Message> GetDraftboxPagging(PagingFeature pf, Guid me)
        {
            var ef = (this.Context as IEntityFrameworkRepositoryContext).Context;
            ef.Configuration.LazyLoadingEnabled = false;

            var pagepredicate = new QuerySupport<Message>().TobuildFilter(pf);


            var query = ef.Set<Message>().Where(x => x.SenderId == me && x.EntityState == Model.EntityStateOperate.IsDraft);
            if (pagepredicate != null)
            {
                query = query.Where(pagepredicate);
            }

            query = query.OrderByDescending(x => x.Id);

            return new PagedResult<Message>(query, pf.PageSize, pf.PageNo);
        }
        /// <summary>
        /// 获取垃圾箱数据
        /// </summary>
        /// <param name="pf"></param>
        /// <param name="me"></param>
        /// <returns></returns>
        public PagedResult<Message> GetDustbinPagging(PagingFeature pf, Guid me)
        {
            var ef = (this.Context as IEntityFrameworkRepositoryContext).Context;
            ef.Configuration.LazyLoadingEnabled = false;

            var pagepredicate = new QuerySupport<Message>().TobuildFilter(pf);

            var query = ef.Set<Message>().Where(x => x.SenderId == me && x.EntityState == Model.EntityStateOperate.IsDraft);
            if (pagepredicate != null)
            {
                query = query.Where(pagepredicate);
            }

            query = query.OrderByDescending(x => x.Id);

            return new PagedResult<Message>(query, pf.PageSize, pf.PageNo);
        }
    }
}